package com.bhs.sansonglogistics.ui.other;

import android.graphics.Color;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.viewpager2.widget.ViewPager2;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseFragment;
import com.bhs.sansonglogistics.bean.DataBoardPlatform;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.consumptionCoupon.CouponDetailListFragment;
import com.bhs.sansonglogistics.ui.home.FragmentAdapter;
import com.bhs.sansonglogistics.utils.DateUtils;
import com.bhs.sansonglogistics.view.MyMarkerView;
import com.bhs.sansonglogistics.view.SelectedFont2;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 数据看板平台
 */
public class DataBoardPlatformFragment extends BaseFragment implements NetCallBack {

    private LineChart line;
    private TextView mTvActive;
    private TextView mTvSigningLine;
    private TabLayout mTabLayout;
    private TextView mTvLeft;
    private TextView mTvRight;
    private ViewPager2 mViewPage;

    @Override
    public int initLayout() {
        return R.layout.fragment_data_board_platform;
    }

    @Override
    public void prestrain() {
        super.prestrain();

        line = view.findViewById(R.id.line);
        line.setNoDataText("暂无数据");   // 没有数据时样式
        line.invalidate();
    }

    @Override
    public void initView(View root) {
        mTvActive = root.findViewById(R.id.tv_active);
        mTvSigningLine = root.findViewById(R.id.tv_signingLine);
        mTabLayout = root.findViewById(R.id.tabLayout);
        mTvLeft = root.findViewById(R.id.tv_left);
        mTvRight = root.findViewById(R.id.tv_right);
        mViewPage = root.findViewById(R.id.view_page);
        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        mTabLayout.addOnTabSelectedListener(new SelectedFont2(activity));
    }

    public static DataBoardPlatformFragment newInstance() {
        return new DataBoardPlatformFragment();
    }


    @Override
    public void initData() {
        networkRequest(netApi.get_platform_report_nums(), this);
        loadingPopupShow();
        ArrayList<BaseFragment> fragments = new ArrayList<>();
        fragments.add(DataBoardPlatformListFragment.newInstance(1));
        fragments.add(DataBoardPlatformListFragment.newInstance(2));
        mViewPage.setOffscreenPageLimit(fragments.size());
        mViewPage.setAdapter(new FragmentAdapter(getActivity(), fragments));
        new TabLayoutMediator(mTabLayout, mViewPage, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(position == 0 ? "活跃货主" : "入驻物流");
            }
        }).attach();
        mViewPage.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                if (position == 0) {
                    mTvLeft.setText("姓名");
                    mTvRight.setText("手机号");
                } else {
                    mTvLeft.setText("公司名称");
                    mTvRight.setText("");
                }
            }
        });
    }


    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        DataBoardPlatform boardPlatform = new Gson().fromJson(json, DataBoardPlatform.class);
        loadingPopupDismiss();
        if (boardPlatform.isStatus()) {
            mTvActive.setText(boardPlatform.getData().getActive_num());
            mTvSigningLine.setText(boardPlatform.getData().getLine_num());
            lineChart(boardPlatform.getData().getOrder_list());
            EventBus.getDefault().postSticky(boardPlatform);
        }
    }

    private void lineChart(List<DataBoardPlatform.DataDTO.OrderListDTO> order_list) {
        List<Entry> list = new ArrayList<>();
        List<Integer> times = new ArrayList<>();
        for (int i = 0; i < order_list.size(); i++) {
            list.add(new Entry(i, order_list.get(i).getNum()));     //其中两个数字对应的分别是   X轴   Y轴
            times.add(order_list.get(i).getDate());
        }


        //list是你这条线的数据  "语文" 是你对这条线的描述（也就是图例上的文字）
        LineDataSet lineDataSet = new LineDataSet(list, "");
        LineData lineData = new LineData(lineDataSet);
        line.setData(lineData);
        XAxis xAxis = line.getXAxis();
        xAxis.setDrawGridLines(false);  //是否绘制X轴上的网格线（背景里面的竖线）
        //   X轴所在位置   默认为上面
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                return DateUtils.getMonthDay(times.get((int) value));
            }
        });
        line.setScaleEnabled(false);
        line.getDescription().setEnabled(false);                  //是否显示右下角描述
        //图例
        Legend legend = line.getLegend();
        legend.setEnabled(false);    //是否显示图例
        lineDataSet.setColor(Color.parseColor("#979797"));  //折线的颜色
        //是否画折线点上的空心圆  false表示直接画成实心圆
        lineDataSet.setDrawCircleHole(false);
        lineDataSet.setCircleColor(Color.parseColor("#979797"));
        lineDataSet.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                return String.valueOf((int) value);
            }
        });
        lineDataSet.setDrawValues(true);
        MyMarkerView mv = new MyMarkerView(getContext(), R.layout.custom_marker_view, times);
        mv.setChartView(line);
        line.setMarker(mv);

        //隐藏右边的Y轴
        line.getAxisRight().setEnabled(false);
        line.getAxisLeft().setDrawAxisLine(false);
        line.setBorderColor(Color.GREEN); // 边框颜色
        line.invalidate();

    }

    @Override
    public void onFailure(int requestCode) {
        loadingPopupDismiss();
    }
}